package com.marchsoft.group.manager.security.handler;

import com.alibaba.fastjson.JSON;
import com.marchsoft.group.manager.common.enums.ResultEnum;
import com.marchsoft.group.manager.common.response.Result;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * fetch
 * @Author zhanghaoqi
 * @Description  登陆失败的处理类
 * @Data 9:58 2020/12/19
 */
@Component
public class MyAuthenticationFailHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e)
            throws IOException, ServletException {
        System.out.println("登录失败");
        httpServletResponse.setContentType("application/json");
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.getWriter().write(JSON.toJSONString(Result.error(ResultEnum.LOGIN_FAIL)));
        httpServletResponse.getWriter().flush();
    }
}
